#include "QtSequentialAnimationEffect.h"


QtSequentialAnimationEffect::QtSequentialAnimationEffect()
{
}

QtSequentialAnimationEffect::QtSequentialAnimationEffect(const QtSequentialAnimationEffect& rhs)
{
	std::list<QtAnimationEffect*>::const_iterator it;

	for(it = rhs.effects.begin(); it != rhs.effects.end(); ++it)
	{
		QtAnimationEffect *effect = *it;

		effects.push_back( effect->Clone() );
	}
}

QtSequentialAnimationEffect::~QtSequentialAnimationEffect()
{
	std::list<QtAnimationEffect*>::iterator it;

	for(it = effects.begin(); it != effects.end(); ++it)
	{
		delete (*it);
	}

	effects.clear();
}

void QtSequentialAnimationEffect::SetDuration(unsigned int newDuration)
{
	std::list<QtAnimationEffect*>::iterator it;

	for(it = effects.begin(); it != effects.end(); ++it)
	{
		(*it)->SetDuration(newDuration);
	}
}

void QtSequentialAnimationEffect::Start()
{
	if (effects.size())
	{
		QtAnimationEffect *front = effects.front();
		QObject::connect(front, SIGNAL(Finished(QtAnimationEffect*)), this, SLOT(EffectFinished(QtAnimationEffect*)));

		front->Start();

		EmitStarted();
	}
}

void QtSequentialAnimationEffect::AddEffect(QtAnimationEffect *effect)
{
	QtAnimationEffect *clone = effect->Clone();

	effects.push_back( clone );
}


void QtSequentialAnimationEffect::EffectFinished(QtAnimationEffect *effect)
{
	std::list<QtAnimationEffect*>::iterator it;

	for(it = effects.begin(); it != effects.end(); ++it)
	{
		if ( effect == (*it) )
		{
			effects.erase(it);
			delete effect;
			break;
		}
	}

	if (effects.size() == 0)
	{
		EmitFinished();
	}
	else
	{
		Start();
	}
}


QtAnimationEffect* QtSequentialAnimationEffect::Clone() const
{
	return new QtSequentialAnimationEffect(*this);
}


